let nums = [1,3,-1,-3,5,3,6,7], k = 3
var maxSlidingWindow = function(nums, k) {
    let arr = []
    let result = []
    for(let i=0;i<k;i++){
        while(arr.length){
            if(nums[i]>nums[arr[arr.length-1]]){
                arr.pop()
            }else{
                arr.push(i)
                break;
            }
        }
        if(arr.length==0){
            arr.push(i)
        }
    }
    result.push(nums[arr[0]])
    for(let j=k;j<nums.length;j++){
        while(arr.length){
            if(arr[0]<=(j-k)){
                arr.shift()
            }
            if(nums[j]>nums[arr[arr.length-1]]){
                arr.pop()
            }else{
                arr.push(j)
                break;
            }
        }
        if(arr.length==0){
            arr.push(j)
        }
            result.push(nums[arr[0]])
    }

    return result
};
console.log(maxSlidingWindow(nums,k));
